'''
爬取的数据: 1.作者 2.标题 3.发布时间 4.文章链接
'''
import requests,json
from bs4 import BeautifulSoup
#1.定义url 和headers
url='https://www.lmonkey.com/t'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'
}
#2.发送请求
res=requests.get(url=url,headers=headers)
#3.判断请求是否成功
if res.status_code==200:

    #4.解析数据
    soup=BeautifulSoup(res.text,'lxml')
    #r=soup.select('div[class="topic_title mb-0 lh-180"]')
    divs=soup.find_all('div',class_="list-group-item list-group-item-action p-06")
    vardata=[]
    for i in divs:
        r=i.find('div',class_='topic_title')
       
        if r:
            vardict={'title':r.text.split('\n')[0],
            'url':i.a['href'],
            'author':i.strong.a.text,
            'publishtime':i.span['title']
            }
            vardata.append(vardict)
    
    #5.存储数据
    with open('./yz.json','w') as fp:
        json.dump(vardata,fp)